# -*- coding: utf-8 -*-
'''
Created on 2011-3-9

@author: Tony

获取与用户互相关注的好友列表
'''

import unittest
from goplay.weibopy.auth import OAuthHandler
from goplay.weibopy.api import API

class getFriends(unittest.TestCase):
    
    consumer_key= "3482834712"
    consumer_secret ="40c54f7f6730f543aed36b36f92ca06e"
    
    def __init__(self):
            """ constructor """
    
    def getAtt(self, key):
        try:
            return self.obj.__getattribute__(key)
        except Exception, e:
            print e
            return ''
        
    def getAttValue(self, obj, key):
        try:
            return obj.__getattribute__(key)
        except Exception, e:
            print e
            return ''
    def auth(self):
        self.auth = OAuthHandler(self.consumer_key, self.consumer_secret)
        auth_url = self.auth.get_authorization_url()
        print 'Please authorize: ' + auth_url
        verifier = raw_input('PIN: ').strip()
        print self.auth.get_access_token(verifier)
        self.api = API(self.auth)
        
    def setToken(self, token, tokenSecret):
        self.auth = OAuthHandler(self.consumer_key, self.consumer_secret)
        self.auth.setToken(token, tokenSecret)
        self.api = API(self.auth)
    #我关注的人
    def friends_ids(self):
        fids = self.api.friends_ids()
        self.obj = fids
        ids = self.getAtt("ids")
        #next_cursor = self.getAtt("next_cursor")
        #previous_cursor = self.getAtt("previous_cursor")
        #print "friends_ids---"+ str(ids) +":next_cursor:"+ str(next_cursor) +":previous_cursor:"+ str(previous_cursor)
        return ids
    
    def friends(self):
        timeline = self.api.friends(count=200)
        '''for line in timeline:
            self.obj = line
            fid = self.getAtt("profile_image_url")
            name = self.getAtt("screen_name")
            print "friends---"+ str(fid) +":"+ name'''
        return timeline
        
    #关注我的人   
    def followers_ids(self):
        fids = self.api.followers_ids()
        self.obj = fids
        ids = self.getAtt("ids")
        #next_cursor = self.getAtt("next_cursor")
        #previous_cursor = self.getAtt("previous_cursor")
        #print "followers_ids---"+ str(ids) +":next_cursor"+ str(next_cursor) +":previous_cursor"+ str(previous_cursor)
        return ids

    def followers(self):
        timeline = self.api.followers(count=200)
        '''for line in timeline:
            self.obj = line
            follower_id=self.getAtt("id")
            fid = self.getAtt("profile_image_url")
            name = self.getAtt("screen_name")
            print "follows---"+ str(fid) +":"+ name'''
        return timeline
    
    #根据id获取用户名
    def userInfo(self,userid):
        user={}
        myself = self.api.get_user(id=userid)            
        self.obj = myself       
        user['username']=self.getAtt("screen_name")
        user['profile_image']=self.getAtt("profile_image_url")

        return user

    def myFriends(self,token,tokenSecret):
        #friends=([])
        friends=''
        gf=getFriends()
        gf.setToken(token, tokenSecret)

        fr_ids=gf.friends_ids()
        print len(fr_ids)
        fo_ids=gf.followers_ids()
        print len(fo_ids)
        if len(fr_ids)>=len(fo_ids):
            for fo_id in fo_ids:
                if fo_id in fr_ids:
                    user=gf.userInfo(fo_id)
                    friends=friends+','+user.get('username')+','+user.get('profile_image')
                    result=friends[1:len(friends)]  
            return result    
        else:
                        
            for fr_id in fr_ids:
                if fr_id in fo_ids:
                    user=gf.userInfo(fr_id)
                    '''friends_name=friends_name+','+user.get('username')
                    friends_image=friends_image+','+user.get('profile_image')
                    n=friends_name[1:len(friends_name)]
                    i=friends_image[1:len(friends_image)]'''
                    friends=friends+','+user.get('username')+','+user.get('profile_image')
                    result=friends[1:len(friends)]         
            return result
                
    
'''gf=getFriends()
u=gf.myFriends("3d6bf54fce077e12f236e67a2ef6e886", "fea2e9c69bae15088bf9aa116c371ea8")
print u'''
#u=gf.auth()
'''uu=[u'\u4f1a\u6ce2', u'http://tp4.sinaimg.cn/1653491083/50/1298649527/1', u'\u5c0f\u738b\u5b50in\u676d\u5dde', u'http://tp2.sinaimg.cn/1745264625/50/1293615810/1', u'lijinyang', u'http://tp2.sinaimg.cn/1627718861/50/1295775332/1', u'Payne-C', u'http://tp3.sinaimg.cn/1852262214/50/1286980738/1', u'\u50cf\u732a\u5934\u5929\u4f7f\u4e00\u6837\u5feb\u4e50', u'http://tp2.sinaimg.cn/1879999481/50/0/0', u'Hugoshine', u'http://tp2.sinaimg.cn/1853655465/50/1289361495/1', u'niu86niu0120', u'http://tp1.sinaimg.cn/1857830132/50/1290610846/0', u'Komelio', u'http://tp3.sinaimg.cn/1735066202/50/1297646226/1', u'hbcomcn', u'http://tp3.sinaimg.cn/1858008102/50/1292994373/1', u'\u5927\u7823', u'http://tp3.sinaimg.cn/1856703490/50/1290436052/1', u'\u7f51\u6613\u624b\u673a\u90ae', u'http://tp3.sinaimg.cn/1850127382/50/1288239644/1', u'\u661f\u91ce\u9614', u'http://tp4.sinaimg.cn/1588879551/50/1286521836/0', u'\u8349\u6839\u8d22\u7ecf', u'http://tp3.sinaimg.cn/1824570010/50/1285059268/1', u'\u7a7f\u9774\u5b50\u7684\u5c71\u732b', u'http://tp1.sinaimg.cn/1828303780/50/1292735771/1', u'PMCamp', u'http://tp2.sinaimg.cn/1771048793/50/1279865953/1', u'\u5c0f\u5a01', u'http://tp4.sinaimg.cn/1134790123/50/1299681901/1', u'wangjinhua', u'http://tp3.sinaimg.cn/1661970550/50/1283755523/1', u'krrishyan', u'http://tp2.sinaimg.cn/1642027101/50/1293276418/1', u'\u5468\u7199lionet', u'http://tp1.sinaimg.cn/1321710092/50/1251372300/1', u'2SIMPLE', u'http://tp3.sinaimg.cn/1689678350/50/1298560104/1', u'Kingerrr', u'http://tp2.sinaimg.cn/1775954033/50/1292684010/1']
uuu=uu[0:len(uu):2]
uuuu=uu[1:len(uu):2]
print uuuu
print uuu
print len(uuuu)
print len(uuu)
print len(uu)'''
#print uuid.uuid1()
#print uuid.uuid3()
#print uuid.uuid4()
#print uuid.uuid5()
#print os.urandom(32)


